package jcommon.chap3_bean;

import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

import jcommon.beans4ex.Job;
import jcommon.beans4ex.Person;

// beanutils 包对 logging 包有依赖
public class SetAndGet {

	/**
	 * 功能: 生成测试数据，依赖PropertyUtils
	 * @author 林可  2010-4-27 下午01:09:14
	 * @return
	 */
	static Person getExamplePersonData() {
		Person person = new Person();
		// person.setName("Alex");
		try {
			PropertyUtils.setProperty(person, "name", "Alex");
			// person.setFavouriteColor(Color.blue);
			PropertyUtils.setProperty(person, "favouriteColor", Color.BLUE);

			// Job job = new Job();
			// person.setJob(job);
			PropertyUtils.setProperty(person, "job", new Job());
			// job.setTitle("Java Engineer");
			// job.setSalary(new BigDecimal("5000.00"));
			PropertyUtils.setProperty(person, "job.title", "Java Engineer");
			PropertyUtils.setProperty(person, "job.salary", new BigDecimal(
					"5000.00"));

			person.addFavouriteFood("火锅");
			person.addFavouriteFood("炝炒藤藤菜");
			// 注意下，像下面的这种情况使用的是list.set()方法，所以无法在没有数据的时候直接添加
			PropertyUtils.setProperty(person, "favouriteFoods[0]", "火锅1");
			PropertyUtils.setProperty(person, "favouriteFoods[1]", "炝炒藤藤菜1");

			Person hisMom = new Person();
			PropertyUtils.setProperty(person, "familyRelation(mother)", hisMom);
			PropertyUtils.setProperty(person, "familyRelation(mother).name",
					"Catalina");

			// person.addFamilyRelation("mother", hisMom);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return person;
	}

	/**
	 * 功能: 生成测试数据，依赖BeanUtils
	 * @author 林可  2010-4-27 下午01:09:14
	 * @return
	 */
	static Person getExamplePersonData2() {
		Person person = new Person();
		// person.setName("Alex");
		try {
			BeanUtils.setProperty(person, "name", "Alex");
			// person.setFavouriteColor(Color.blue);
			BeanUtils.setProperty(person, "favouriteColor", Color.BLUE);

			// Job job = new Job();
			// person.setJob(job);
			BeanUtils.setProperty(person, "job", new Job());
			// job.setTitle("Java Engineer");
			// job.setSalary(new BigDecimal("5000.00"));
			BeanUtils.setProperty(person, "job.title", "Java Engineer");
			BeanUtils.setProperty(person, "job.salary", new BigDecimal(
					"5000.00"));

			person.addFavouriteFood("火锅");
			person.addFavouriteFood("炝炒藤藤菜");
			// 注意下，像下面的这种情况使用的是list.set()方法，所以无法在没有数据的时候直接添加
			BeanUtils.setProperty(person, "favouriteFoods[0]", "火锅1");
			BeanUtils.setProperty(person, "favouriteFoods[1]", "炝炒藤藤菜1");

			Person hisMom = new Person();
			BeanUtils.setProperty(person, "familyRelation(mother)", hisMom);
			BeanUtils.setProperty(person, "familyRelation(mother).name",
					"Catalina");

			// person.addFamilyRelation("mother", hisMom);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return person;
	}

	public static void main(String[] args) throws Exception {
		System.out.println("---------- Test PropertyUtils ----------");
		t1();
		System.out.println("---------- Test BeanUtils ----------");
		t2();
	}

	// PropertyUtils
	private static void t1() {
		Person person = getExamplePersonData();
		try {
			String name;
			name = (String) PropertyUtils.getSimpleProperty(person, "name");
			Color favouriteColor = (Color) PropertyUtils.getSimpleProperty(
					person, "favouriteColor");
			BigDecimal sal = (BigDecimal) PropertyUtils.getNestedProperty(
					person, "job.salary");
			String fFood1 = (String) PropertyUtils.getIndexedProperty(person,
					"favouriteFoods[0]");
			String fFood2 = (String) PropertyUtils.getIndexedProperty(person,
					"favouriteFoods", 1);
			Person mom = (Person) PropertyUtils.getMappedProperty(person,
					"familyRelation", "mother");
			Person mom_ = (Person) PropertyUtils.getMappedProperty(person,
					"familyRelation(mother)");
			
			Class typeOfFavColor = PropertyUtils.getPropertyType(person,
					"favouriteColor");
			Class typeOfFavColor2 = Person.class.getDeclaredField(
					"favouriteColor").getType();

			System.out.println(person);
			System.out.println(name);
			System.out.println(favouriteColor);
			System.out.println(sal);
			System.out.println(fFood1);
			System.out.println(fFood2);
			System.out.println(mom);
			System.out.println(mom_);
			System.out.println(typeOfFavColor.getName());
			System.out.println(typeOfFavColor2.getName());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
	}
	
	private static void t2() {
		Person person = getExamplePersonData();
		try {
			String name = BeanUtils.getSimpleProperty(person, "name");
			String favouriteColor = BeanUtils.getSimpleProperty(
					person, "favouriteColor");
			String sal = BeanUtils.getNestedProperty(
					person, "job.salary");
			String fFood1 = BeanUtils.getIndexedProperty(person,
					"favouriteFoods[0]");
			String fFood2 = BeanUtils.getIndexedProperty(person,
					"favouriteFoods", 1);
			String mom = BeanUtils.getMappedProperty(person,
					"familyRelation", "mother");
			String mom_ = BeanUtils.getMappedProperty(person,
					"familyRelation(mother)");

			System.out.println(person);
			System.out.println(name);
			System.out.println(favouriteColor);
			System.out.println(sal);
			System.out.println(fFood1);
			System.out.println(fFood2);
			System.out.println(mom);
			System.out.println(mom_);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}
	}
}
